/**
 * Created by hsiacool on 2017/8/23.
 */
import e = require('express');
import BaseController from "./BaseController";
import { router } from "../decorators/Web";
import {ArticleDao} from "../dao/index";
import {ArticleTypeDao} from "../dao/index";

class NewsController extends BaseController {

    @router({
        method: 'get',
        path: '/news.html'
    })
    async index(req:any, res: e.Response){
        let pageIndex:number;
        if(req.query.pageIndex){
            pageIndex = req.query.pageIndex;
        }else{
            pageIndex = 0;
        }
        let cid:number;
        ArticleTypeDao.find({
            parentId:0,suppId:1,type:1,delFlag:0, visible:1
        }).then((articleType:any)=>{
            if(req.query.id){
                cid = req.query.id;
            }else{
                cid = articleType[0].cId;
            }
            ArticleDao.find({cId:cid,type:-13,supp_id:1,del_flag:0},Number(pageIndex)*10).then((articleRes:any)=>{
                ArticleTypeDao.find({cId:cid}).then((articleId:any)=>{
                ArticleDao.count({cId:cid,type:-13,supp_id:1,del_flag:0}).then((articleCount:any)=>{
            super.render(req,res,'/news.html',{data:{data1:articleRes,data2:Math.ceil(articleCount/10),data3:articleType,data4:articleId}});
                });
                });
            });
        });
    }
}
export default NewsController
